/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;



/**
 *
 * @author ngocbinh
 */
public class CreateXML 
{
     XMLOutputter printer = null;
    File anXmlDocFile;

    public CreateXML() {
        //lets create an XML Printer!
        printer = new XMLOutputter();
    }

    public CreateXML(File xmlFile) {
        //lets create an XML Printer!
        printer = new XMLOutputter();
        this.anXmlDocFile = xmlFile;
    }
    public void Write() throws JDOMException, IOException {
        SAXBuilder parser = new SAXBuilder();
        try {

            Element domain = new Element("web");
            Document doc = new Document(domain);
            XMLOutputter xmOut = new XMLOutputter();
            xmOut.setFormat(Format.getPrettyFormat());
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(this.anXmlDocFile.getName())));
            String xmlOut = xmOut.outputString(doc);
            bw.write(xmlOut);
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void WriteName(String value1,String value2) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();

            Element e = new Element("name");
            e.setAttribute("url", value1);
            e.setAttribute("id", value2);
            domain.addContent(e);

            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();

            // passsed System.out to see document content on console  
//            xmlOutput.output(doc, System.out);

            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(doc, new FileWriter(
                    this.anXmlDocFile.getName()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
     public void WriteChuyenMuc(String tagName,String value1,String value2) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> lst=domain.getChildren();
            Element eName=null;
            for(int i=0;i<lst.size();i++)
            {
                Element tmp=lst.get(i);
                if(tmp.getAttributeValue("url").equals(tagName))
                {
                    eName=tmp;
                    break;
                }
            }
            Element e = new Element("chuyenmuc");
            e.setAttribute("url", value1);
            e.setAttribute("ten", value2);
            eName.addContent(e);

            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();

            // passsed System.out to see document content on console  
//            xmlOutput.output(doc, System.out);

            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(doc, new FileWriter(
                    this.anXmlDocFile.getName()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void Write(String tenchuyenmuc, int pagenumber) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");

            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(tenchuyenmuc) == true) {
                    e = chuyenmuc.get(i);
                }
            }
            Element page = new Element("page");
            page.setAttribute("number", pagenumber + "");
            e.addContent(page);

            // get object to see output of prepared document  
            XMLOutputter xmlOutput = new XMLOutputter();
            // passed fileWriter to write content in specified file  
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(doc, new FileWriter(
                    this.anXmlDocFile.getName()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
